import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {LoveRecycManageService} from '../../love-recyc-manage.service';
import {ModelBasePriceService} from '../model-base-price.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-model-base-price-edit',
  templateUrl: './model-base-price-edit.component.html',
  styleUrls: ['./model-base-price-edit.component.scss']
})
export class ModelBasePriceEditComponent implements OnInit {
  formData = {
    id: '',
    brandId: '',
    category: '' as any,
    seriesId: '',
    modelId: '',
    note: '',
    content: [] as any,
  };
  brandOptions: any = [];
  seriesOptions: any = [];
  modelOptions: any = [];
  operationType = '';
  iphoneChannel = [
    '大陆国行',
    '国行其他版本',
    '港澳台版',
    '外版无锁',
    '外版有锁',
  ];
  phoneChannel = [
    '大陆国行',
    '国行其他版本',
    '港澳台版',
    '其他地区/国家版本',
  ];
  ipadChannel = [
    '大陆国行',
    '国行其他版本',
    '港澳台版',
    '外版无锁',
    '配置锁/监管设备',
  ];

  constructor(
    private activatedRoute: ActivatedRoute,
    private loveRecycManageService: LoveRecycManageService,
    private modelBasePriceService: ModelBasePriceService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    let data = this.activatedRoute.snapshot.queryParams?.data;
    const type = this.activatedRoute.snapshot.queryParams?.type;
    if (data) {
      this.operationType = type;
      data = JSON.parse(data);
      this.formData = {
        id: data.id,
        brandId: data.brandId,
        category: data.category,
        seriesId: data.seriesId,
        modelId: data.modelId,
        note: data.note,
        content: JSON.parse(data.content),
      };
      this.getSeriesOptions();
      this.getModelOptions();
    }
    this.loveRecycManageService.getRecycleBrandList({page: 1, pageSize: 99999}).then((res: any) => {
      this.brandOptions = res.data.records;
    });
  }

  brandChange() {
    this.formData.seriesId = '';
    this.formData.modelId = '';
    this.seriesOptions = [];
    this.modelOptions = [];
    this.getSeriesOptions();
    this.formData.content = [];
  }

  categoryChange() {
    this.formData.seriesId = '';
    this.formData.modelId = '';
    this.seriesOptions = [];
    this.modelOptions = [];
    this.getSeriesOptions();
    this.formData.content = [];
  }

  seriesChange() {
    this.formData.modelId = '';
    this.modelOptions = [];
    this.getModelOptions();
    this.formData.content = [];
  }

  modelChange() {
    this.formData.content = [];
  }

  getSeriesOptions() {
    this.loveRecycManageService.getRecycleSeriesList({
      brandId: this.formData.brandId,
      category: this.formData.category,
      page: 1,
      pageSize: 99999
    }).then((res: any) => {
      this.seriesOptions = res.data.records;
    });
  }

  getModelOptions() {
    this.loveRecycManageService.getRecycleModelList({
      brandId: this.formData.brandId,
      category: this.formData.category,
      seriesId: this.formData.seriesId,
      page: 1,
      pageSize: 99999
    }).then((res: any) => {
      this.modelOptions = res.data.records;
    });
  }

  generateTable() {
    const memoryList = this.modelOptions.filter((item: any) => item.id === this.formData.modelId)[0].memory?.split(',') || [];
    if (!memoryList.length) {
      this.nzMessageService.error('该机型没有配置内存');
      return;
    }
    let channel: any[] = [];
    if (this.formData.category * 1 === 1) {
      if (this.brandOptions.filter((item: any) => item.id === this.formData.brandId)[0].name === '苹果') {
        channel = this.iphoneChannel;
      } else {
        channel = this.phoneChannel;
      }
    }
    if (this.formData.category * 1 === 2) {
      channel = this.ipadChannel;
    }
    channel = channel.map((item: any) => ({
      channel: item,
      list: memoryList.map((m: any) => ({
        romNum: m,
        zb: '',
        zbC: '',
        zbsc: '',
        gb: '',
        gbC: '',
        gbsc: ''
      }))
    }));
    if (this.formData.content.length) {
      channel.forEach((item: any) => {
        item.list = item.list.map((m: any) => {
          let result = m;
          this.formData.content.forEach((oldItem: any) => {
            oldItem.list.forEach((oldM: any) => {
              result = item.channel === oldItem.channel && m.romNum === oldM.romNum ? oldM : result;
            });
          });
          return result;
        });
      });
    }
    this.formData.content = channel;
  }

  save() {
    if (!this.formData.brandId) {
      this.nzMessageService.error('请选择品牌');
      return;
    }
    if (!this.formData.category) {
      this.nzMessageService.error('请选择品类');
      return;
    }
    if (!this.formData.seriesId) {
      this.nzMessageService.error('请选择系列');
      return;
    }
    if (!this.formData.modelId) {
      this.nzMessageService.error('请选择机型');
      return;
    }
    this.formData.content.forEach((c: any) => {
      c.list.forEach((m: any) => {
        m.zbsc = m.zbC !== m.zb ? m.zbC : m.zbsc;
        m.zbC = m.zb;
        m.gbsc = m.gbC !== m.gb ? m.gbC : m.gbsc;
        m.gbC = m.gb;
      });
    });
    this.modelBasePriceService.modifyModelBasePriceList({
      ...this.formData,
      content: JSON.stringify(this.formData.content)
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        window.history.back();
      }
    });
  }
}
